package cn.mill.demo.api;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;

@Component
public class EmbeddedServerConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
	private static Logger logger = LoggerFactory.getLogger(EmbeddedServerConfig.class);
	
	@Override
	public void customize(ConfigurableServletWebServerFactory factory) {
		logger.info("=====================EmbeddedServerConfig Start================================");
		((TomcatServletWebServerFactory) factory).addConnectorCustomizers(new TomcatConnectorCustomizer() {
			@Override
			public void customize(Connector connector) {
				Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
				protocol.setMaxConnections(16 * 10000);
				protocol.setMaxThreads(6 * 100);
				protocol.setSelectorTimeout(3000);
				protocol.setSessionTimeout(3000);
				protocol.setConnectionTimeout(3000);
			}
		});
		((TomcatServletWebServerFactory) factory).addContextCustomizers(new TomcatContextCustomizer() {
			@Override
			public void customize(Context context) {
				context.addWelcomeFile("/index.html");
				logger.info("=====================Add a Welcome File index.html================================");
			}
		});
		logger.info("=====================EmbeddedServerConfig End ================================");
	}
	
}
